<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递单号" prop="waybillNo">
        <el-input
          v-model="queryParams.waybillNo"
          placeholder="请输入快递单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="收件人" prop="receiver">
        <el-input
          v-model="queryParams.receiver"
          placeholder="请输入收件人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobileNumber">
        <el-input
          v-model="queryParams.mobileNumber"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发货时间" prop="sendTime">
        <el-input
          v-model="queryParams.sendTime"
          placeholder="请输入发货时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货时间" prop="receiveTime">
        <el-input
          v-model="queryParams.receiveTime"
          placeholder="请输入收货时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="快递单号" align="center" prop="waybillNo" />
      <el-table-column label="订单状态" :formatter="statusFormat" align="center" prop="status" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品数量" align="center" prop="goodsCount" />
      <el-table-column label="商品规格" align="center" prop="goodsSpec" />
      <el-table-column label="支付总价" align="center" prop="totalPrice" />
      <el-table-column label="运费" align="center" prop="freight" />
      <el-table-column label="商品图片" align="center" prop="goodsPic" />
      <el-table-column label="收件人" align="center" prop="receiver" />
      <el-table-column label="手机号" align="center" prop="mobileNumber" />
      <el-table-column label="地址" align="center" prop="province" />
      <el-table-column label="发货时间" align="center" prop="sendTime" />
      <el-table-column label="收货时间" align="center" prop="receiveTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >修改</el-button>-->
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:delete']"
          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSend(scope.row)"
            v-if="scope.row.status ===10"
          >发货</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="快递单号" prop="waybillNo">
          <el-input v-model="form.waybillNo" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="商品主键" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品主键" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品数量" prop="goodsCount">
          <el-input v-model="form.goodsCount" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="商品单价" prop="goodsPrice">
          <el-input v-model="form.goodsPrice" placeholder="请输入商品单价" />
        </el-form-item>
        <el-form-item label="商品规格" prop="goodsSpec">
          <el-input v-model="form.goodsSpec" placeholder="请输入商品规格" />
        </el-form-item>
        <el-form-item label="支付总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入支付总价" />
        </el-form-item>
        <el-form-item label="运费" prop="freight">
          <el-input v-model="form.freight" placeholder="请输入运费" />
        </el-form-item>
        <el-form-item label="商品图片" prop="goodsPic">
          <el-input v-model="form.goodsPic" placeholder="请输入商品图片" />
        </el-form-item>
        <el-form-item label="c_m_user表cd" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入c_m_user表cd" />
        </el-form-item>
        <el-form-item label="收件人" prop="receiver">
          <el-input v-model="form.receiver" placeholder="请输入收件人" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNumber">
          <el-input v-model="form.mobileNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="省编码" prop="provinceCode">
          <el-input v-model="form.provinceCode" placeholder="请输入省编码" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区" prop="area">
          <el-input v-model="form.area" placeholder="请输入区" />
        </el-form-item>
        <el-form-item label="详细地址" prop="detailedAddress">
          <el-input v-model="form.detailedAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="创建用户id" prop="createUserId">
          <el-input v-model="form.createUserId" placeholder="请输入创建用户id" />
        </el-form-item>
        <el-form-item label="发货时间" prop="sendTime">
          <el-input v-model="form.sendTime" placeholder="请输入发货时间" />
        </el-form-item>
        <el-form-item label="收货时间" prop="receiveTime">
          <el-input v-model="form.receiveTime" placeholder="请输入收货时间" />
        </el-form-item>
        <el-form-item label="删除标记 : 0 正常 , 1已删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记 : 0 正常 , 1已删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/system/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        waybillNo: null,
        status: null,
        goodsId: null,
        goodsName: null,
        goodsCount: null,
        goodsPrice: null,
        goodsSpec: null,
        totalPrice: null,
        freight: null,
        goodsPic: null,
        userId: null,
        receiver: null,
        mobileNumber: null,
        provinceCode: null,
        province: null,
        city: null,
        area: null,
        detailedAddress: null,
        createUserId: null,
        sendTime: null,
        receiveTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        goodsId: [
          { required: true, message: "商品主键不能为空", trigger: "blur" }
        ],
        goodsCount: [
          { required: true, message: "商品数量不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "c_m_user表cd不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        waybillNo: null,
        status: 0,
        goodsId: null,
        goodsName: null,
        goodsCount: null,
        goodsPrice: null,
        goodsSpec: null,
        totalPrice: null,
        freight: null,
        goodsPic: null,
        userId: null,
        receiver: null,
        mobileNumber: null,
        provinceCode: null,
        province: null,
        city: null,
        area: null,
        detailedAddress: null,
        createTime: null,
        createUserId: null,
        sendTime: null,
        receiveTime: null,
        delFlag: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 发货按钮操作 */
    handleSend(row) {
      this.$modal.confirm('是否确认发货？').then(function() {
        return sendOrder(row);
      }).then(() => {
        this.getList();
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    statusFormat (row) {
      switch (row.status) {
        case 10:
          return "待发货";
        case 20:
          return "待收货";
        case 30:
          return "已收货";
        case 40:
          return "待退货";
        case 50:
          return "已取消";
      }
    },
  }
};
</script>
